package com.school.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mhuang.school.starter.minio.core.MinioFrame;
import com.mhuang.school.starter.minio.util.ToolUtil;
import com.school.device.config.DahuaCofig;
import com.school.device.door.DoorSdk;
import com.school.device.dynamic.DvIntellifHikvisonDevice;
import com.school.device.util.DataConvertUtil;
import com.school.device.util.JsonUtil;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping(value = "/accessControlFace/door/doorApi", name = "后台部门管理")
public class DoorController {

	@Autowired
	private DahuaCofig dahuaCofig;
	
	private List<DvIntellifHikvisonDevice> getDevices(String deviceIpUrl) {
		if (ToolUtil.isEmpty(deviceIpUrl)) {
			System.err.println("拉取设备端口接口为空");
			log.error("拉取设备端口接口为空");
			return null;
		}
		HttpResponse response = HttpRequest.get(deviceIpUrl).execute();
		if (response.isOk()) {
			String body = response.body();
			if (ToolUtil.isNotEmpty(body)) {
				Map<String, Object> result = JsonUtil.toMap(body);
				if (DataConvertUtil.getObjectStr(result, "respCode").equalsIgnoreCase("10000000"))
					return JsonUtil.toBeans(JSON.toJSONString(result.get("data")), DvIntellifHikvisonDevice.class);
			}
		}
		System.err.println("请检查连接设备端口");
		log.error("请检查连接设备端口");
		return null;
	}
	
	@Autowired
	private MinioFrame minioFrame;

	@GetMapping(value = "/getPager", name = "获取全部树#{4;1;}", produces = "application/json;charset=utf-8")
	public BaseResponse getPager(@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize) throws Exception {
		if(page==null)
			page=1;
		if(pageSize==null)
			pageSize=8;	
		pageSize=100;
		List<DvIntellifHikvisonDevice> devices = getDevices(dahuaCofig.getDeviceipUrl());				
		devices = devices.stream().filter(item->(item.getDeviceType()==5)).collect(Collectors.toList());
		if(devices!=null && devices.size()>0) {
			DvIntellifHikvisonDevice device=devices.get(0);
			PagerInfo<Map<String, Object>>  pagerInfo=new DoorSdk(device,minioFrame).start(page,pageSize);
			List<Map<String, Object>> datas=pagerInfo.getList();
			if(datas!=null) {
				datas.forEach((item)->{
					item.put("deviceName",device.getDeviceName());
					item.put("deviceType",device.getDeviceType());
					item.put("deviceAddress",device.getDeviceAddress());
				});	
			}	
			return BaseResponse.ok(pagerInfo);
		}else {
			return BaseResponse.ok();
		}

	}

	@PostMapping("/receive")
	public void receive(@RequestBody JSONObject jsonObject) {
		if (ObjectUtil.isNotEmpty(jsonObject)) {
			log.info("主动推送{}", jsonObject);
		}
	}
}
